import com.rabbitmq.client.*;

import java.io.IOException;

/**
 * @Author： yu.Tan
 * @Date: 2020/8/19 16:55
 * @Description:
 */
public class ConsumerWorkQueueB {
    private static final String WORK_QUEUE_NAME = "MQ_WORK_QUEUE";

    public static void main(String[] args) {
        //获取连接
        Connection connection = MQConnecitonUtils.getConnection();

        Channel channel=null;

        try {

            //创建连接
            channel=connection.createChannel();

            //声明队列
            channel.queueDeclare(WORK_QUEUE_NAME,false,false,false,null);

            //创建消费者
            DefaultConsumer consumer=new DefaultConsumer(channel){
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                    //获取消费者信息
                    String msg = new String(body, "UTF-8");
                    System.out.println("【ConsumerWorkQueueB】 receive message:"+msg);

                    //模拟延迟
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            };
            channel.basicConsume(WORK_QUEUE_NAME,true,consumer);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        }
    }
}
